﻿using com.guojin.core.io.message;
using com.guojin.mj.net.message.game;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


public  class OnPayChatItemClick : MonoBehaviour, IPointerClickHandler
{
    public int ID = 0;
    public int price = 0;
    public int receiveLocationIndex;


    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("发送chat字符串 :" + ID);

        MessageManager.GetInstance().SendMsg1251(ID,  price, GameData.GetInstance().locationIndex, receiveLocationIndex);
        Destroy(gameObject.transform.root.FindChild("addNode/GameRoomPage(Clone)/FightAddNode/PlayerInfoPage(Clone)").gameObject);
    }
}

public class PlayerInfoPage : EventManager {



    public override void InformationSetting()
    {
        AddListenerToButton(closeBtn, CloseBtnClick);
        AddListenerToButton(outBtn, OutBtnClick);
    }

    public GameObject content;
    public RawImage headImg;
    public Text name;
    public Button outBtn;
    public Button closeBtn;
    public Text gold;


    public void OnReceiveMsg1254(string msg, Message data)
    {
        CheckGoldReq cg = (CheckGoldReq) data;

        if (cg.UserID == playerUserId)
        {
            SetText(gold, cg.Gold.ToString());
        }
    }

    private int playerUserId;
    public  void Init(Player player)
    {
        playerUserId = player.userId;

        headImg.texture = player.headTexture;

        SetText(name, player.name);

        if (player.locationIndex  != GameData.GetInstance().locationIndex )
        {
            if (GameData.GetInstance().userID == GameData.GetInstance().roomHostUserID)
               SetOutBtn(true);
            else
                SetOutBtn(false);

            gold.gameObject.SetActive(false);

            for (int i = 0; i < content.transform.childCount; i++)
            {
                content.transform.GetChild(i).gameObject.AddComponent<OnPayChatItemClick>().ID = int.Parse(content.transform.GetChild(i).name);
                content.transform.GetChild(i).gameObject.GetComponent<OnPayChatItemClick>().price =
                    int.Parse(content.transform.GetChild(i).FindChild("Text").GetComponent<Text>().text);
                content.transform.GetChild(i).gameObject.GetComponent<OnPayChatItemClick>().receiveLocationIndex =
                    player.locationIndex;
            }
        }
        else
        {
            MessageManager.GetInstance().SendMsg1254(playerUserId, 0);
            SetOutBtn(false);
            gold.gameObject.SetActive(true);
            content.gameObject.SetActive(false);
        }

        if (GameData.GetInstance().locationIndex == -1)
        {
            content.gameObject.SetActive(false);
        }

    }

    private void SetOutBtn(bool show)
    {
        if (show)
        {
            outBtn.gameObject.SetActive(true);
        }
        else
        {
            outBtn.gameObject.SetActive(false);
        }
    }

    public void OutBtnClick()
    {
        MessageManager.GetInstance().SendMsg1249(GameData.GetInstance().userName, int.Parse(GameData.GetInstance().userID),  playerUserId);
    }
}
